package com.qumitech.visualnovel.controller.rest;

import com.alibaba.fastjson.JSONObject;

import com.qumitech.visualnovel.model.common.BaseRsp;
import com.qumitech.visualnovel.model.common.ResultEnum;
import com.qumitech.visualnovel.model.common.BaseResponse;

import com.qumitech.visualnovel.pubdef.dto.associate.AssociateAccountDTO;
import com.qumitech.visualnovel.service.associate.AssociateAccountService;
import com.google.common.collect.ImmutableMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/api/associateAccount")
public class AssociateAccountController {

    @Autowired
    AssociateAccountService associateAccountService;

    @RequestMapping(value = "registerRank", method = RequestMethod.GET)
    public JSONObject registerRank(@RequestParam(value="startTime",required=false)String startTime,
                                   @RequestParam(value="endTime",required=false)String endTime){
        JSONObject res = associateAccountService.registerRank(startTime,endTime);
        return new BaseResponse<>(0,"success", res);
    }

    @RequestMapping(value = "loginRank", method = RequestMethod.GET)
    public JSONObject loginRank(@RequestParam(value="startTime",required=false)String startTime,
                                @RequestParam(value="endTime",required=false)String endTime){
        JSONObject res = associateAccountService.loginRank(startTime,endTime);
        return new BaseResponse<>(0,"success", res);
    }

    @RequestMapping(value = "payRank", method = RequestMethod.GET)
    public JSONObject payRank(@RequestParam(value="startTime",required=false)String startTime,
                              @RequestParam(value="endTime",required=false)String endTime)
    {
        JSONObject res = associateAccountService.payRank(startTime,endTime);
        return new BaseResponse<>(0,"success", res);
    }
    @RequestMapping(value = "", method = RequestMethod.GET)
    public BaseRsp listAccountByDimension(@RequestParam(value = "dimension") String dimension,
                                          @RequestParam(value = "dimensionValue") String dimensionValue,
                                          @RequestParam(value="startTime",required=false)String startTime,
                                          @RequestParam(value="endTime",required=false)String endTime,
                                          @RequestParam(value="orderType", required=false, defaultValue="DESC") String orderType,
                                          @RequestParam(value="orderBy", required=false, defaultValue="record_time") String orderBy,
                                          @RequestParam(value="offset", required=false, defaultValue="0") Integer offset,
                                          @RequestParam(value="limit", required=false, defaultValue="20") Integer limit){
        List<AssociateAccountDTO> associateAccountDTOList = associateAccountService.listAccountByDimension(dimension, dimensionValue, startTime, endTime, orderType, orderBy,offset,limit);
        int count = associateAccountService.countAccountByDimension(dimension,dimensionValue, startTime, endTime);
        return new BaseRsp(ResultEnum.SUCCESS, ImmutableMap.of("associate_account", associateAccountDTOList, "count", count));
    }

    @RequestMapping(value = "rank-list", method = RequestMethod.GET)
    public BaseRsp listRankByDimension(@RequestParam(value = "dimension") String dimension,

                                          @RequestParam(value="startTime",required=false)String startTime,
                                          @RequestParam(value="endTime",required=false)String endTime,
                                          @RequestParam(value="orderType", required=false, defaultValue="DESC") String orderType,
                                          @RequestParam(value="orderBy", required=false, defaultValue="record_time") String orderBy,
                                          @RequestParam(value="offset", required=false, defaultValue="0") Integer offset,
                                          @RequestParam(value="limit", required=false, defaultValue="20") Integer limit){
        List<HashMap<String, String>> rank = associateAccountService.listRankByDimension(dimension, startTime, endTime, orderType, orderBy,offset,limit);
        int count = associateAccountService.countRankByDimension(dimension, startTime, endTime);
        return new BaseRsp(ResultEnum.SUCCESS, ImmutableMap.of("rank_list", rank, "count", count));
    }
}
